package org.stars.dao.sqlmapper.parameters;

import org.stars.dao.sqlmapper.Parameter;
import org.stars.dao.sqlmapper.ParameterType;

public abstract class ParameterParser {

	/**
	 * Parse an input string to define kind of parameter.
	 * 
	 * How to detect kind:
	 * <ul>
	 * 		<li>If input begins with "$user." is an USER_PARAMETER.</li>
	 * 		<li>If input begins with "$config." is an CONFIG_PARAMETER.</li>
	 * 		<li>If input begins with "$dto." or with nothing is an DTO_PARAMETER.</li>
	 * </ul>
	 * 
	 * @param input
	 * @return
	 */
	public static Parameter parse(String input)
	{
		Parameter ret=new Parameter();
		input=input.toLowerCase();
		
		if (input.startsWith("$user."))
		{
			ret.setType(ParameterType.TYPE_USER_ATTRIBUTE);
			ret.setName(input.substring(input.indexOf(".")+1));			
		} else if (input.startsWith("$config.")) {
			ret.setType(ParameterType.TYPE_CONFIG_ATTRIBUTE);
			ret.setName(input.substring(input.indexOf(".")+1));
		} else if (input.startsWith("$dto.")) {
			ret.setType(ParameterType.TYPE_USER_ATTRIBUTE);
			ret.setName(input.substring(input.indexOf(".")+1));
		} else {
			ret.setName(input);
		}
		
		return ret;
	}
}
